package com.fingard.dsp.bank.directbank.cmb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IInvoiceMsg;
import com.fingard.dsp.bank.reqrespobj.invoicemsg.ReqInvoiceMsgQry;
import com.fingard.dsp.bank.reqrespobj.invoicemsg.RetAtsInvoiceMsg;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2019/9/21.
 */
public class CMB01InvoiceMsg extends CMB01Base implements IInvoiceMsg {
    @Override
    public RetAtsInvoiceMsg getInvoiceMsg(ReqInvoiceMsgQry qry) throws Exception {
        RetAtsInvoiceMsg retAts = new RetAtsInvoiceMsg(reqHeader);
        retAts.writeStart();
        try {
            CMB01Header header = new CMB01Header();
            header.FUNNAM = "TDAFFPRV";
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, header);
//            输入	TDAFFPRVX	输入属性	单记录
            xmlWriter.writeStartElement("TDAFFPRVX");
//            直连对接企业系统客户号	COREID	C(10)	否	长安主机厂对应的10位客户号
            xmlWriter.writeElementString("COREID", bankFront.org);
//            业务参考号	YURREF	C(30)	否
            xmlWriter.writeElementString("YURREF", header.FUNNAM+getYURREF(reqHeader.transSeqID));
            //            保留字段	REV50L	C(50)	可
            xmlWriter.writeElementString("REV50L", "");
            xmlWriter.writeEndElement();
            while (qry.readRecord()) {
//                输入	TDAFFPRVY	输入属性	多记录
                xmlWriter.writeStartElement("TDAFFPRVY");
//                发票号码	INVNUM	C (10)	否t
                xmlWriter.writeElementString("INVNUM",qry.reqInvoiceMsgDetail.InvoiceNo);
//                发票代码	INVCOD	C (20)	否
                xmlWriter.writeElementString("INVCOD", qry.reqInvoiceMsgDetail.InvoiceCode);
//                经销商名称	DLENAM	Z(100)	否	经销商名称
                xmlWriter.writeElementString("DLENAM", qry.reqInvoiceMsgDetail.BillDealerName);
//                开票日期	BILDAT	C(20)	否	精确到时分秒 格式：YYYY-MM-DD HH:MM:SS (中间空格分隔)
                xmlWriter.writeElementString("BILDAT", Format.DateTimeFormat.format(Format.Date8Format.parse(qry.reqInvoiceMsgDetail.InvoiceDate)));
//                底盘号/车架号	CHANUM	C(40)	否
                xmlWriter.writeElementString("CHANUM",qry.reqInvoiceMsgDetail.VIN );
//                开票价格	BILPRE	M	否
                xmlWriter.writeElementString("BILPRE", qry.reqInvoiceMsgDetail.InvoicePrice);
//                发票状态	INVSTS	C(2)	否	01-开票作废、02-正数发票、03-负数发票
                if("0".equals(qry.reqInvoiceMsgDetail.InvoiceStatus)){
                    xmlWriter.writeElementString("INVSTS","02" );
                }else if("1".equals(qry.reqInvoiceMsgDetail.InvoiceStatus)){
                    xmlWriter.writeElementString("INVSTS","03" );
                }else {
                    xmlWriter.writeElementString("INVSTS","01" );
                }
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());

            if(tmpStrRet[0].length() == 0) {
                StringBuilder tmpbkMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpbkMsg);
                if (tmpBkReader != null) {
                    String bankRetCode = tmpBkReader.getFirstText("/CMBSDKPGK/INFO/RETCOD");
                    if("0".equals(bankRetCode)) {
//                        TDAFFPRVZ
//                        返回代码	RTNCOD	C(7)	否	返回值：SCDS000则代表成功 其他值则代表失败
//                        返回信息	RTNMSG	Z(256)	可
                        String rtnCod = tmpBkReader.getFirstText("/CMBSDKPGK/TDAFFPRVZ/RTNCOD");
                        String rtnMsg = tmpBkReader.getFirstText("/CMBSDKPGK/TDAFFPRVZ/RTNMSG");
                        if("SCDS000".equals(rtnCod)) {
                            retAts.respCode ="0";
                            retAts.respInfo = rtnMsg;
                        }else{
                            throw new Exception(rtnCod+":"+rtnMsg);
                        }

                    }else{
                        String bankRetMsg =tmpBkReader.getFirstText("/CMBSDKPGK/INFO/ERRMSG");
                        throw new Exception(bankRetCode+":"+bankRetMsg);
                    }
                }else {
                    throw new Exception(tmpBkReader.toString());
                }
            } else{
                throw new Exception(tmpStrRet[0]);
            }
        }catch (Exception ex){
            ex.printStackTrace();
            retAts.respCode ="-1";
            retAts.respInfo = ex.getMessage();
        }
        retAts.writeRespCode(retAts.respCode);
        retAts.writeRespInfo(retAts.respInfo);
        retAts.writeEnd();
        return retAts;
    }
}
